package com.tduck.cloud.api.web.job;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.tduck.cloud.account.entity.UserEntity;
import com.tduck.cloud.account.service.UserService;
import com.tduck.cloud.api.util.UipUtils;
import com.tduck.cloud.common.constant.UipConstant;
import com.tduck.cloud.common.util.uip.model.UipMessageTemplate;
import com.tduck.cloud.form.entity.RelationshipEntity;
import com.tduck.cloud.form.entity.UserFormEntity;
import com.tduck.cloud.form.service.RelationshipService;
import com.tduck.cloud.form.service.UserFormService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 推送问卷地址及密码
 *
 * @author 69562
 * @date 2023/05/04
 */
@Slf4j
public class SendPlainTextJob implements Job {
    private final String MSG_CONTENT = "您本次需要填写的答卷是《%s》,点击开始测评";

    @Resource
    private RelationshipService relationshipService;
    @Resource
    private UserService userService;
    @Resource
    private UipUtils uipUtils;
    @Resource
    private UserFormService userFormService;
    @Value("${tduck.url}")
    private String url;
    @Value("${spring.profiles.active}")
    private String env;

    private static final String ENV_DEV = "dev";

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap jobDataMap = context.getMergedJobDataMap();
        String formKey = jobDataMap.getString("parameter");
        UserFormEntity userFormEntity = userFormService.query().eq("form_key", formKey).one();
        Assert.notNull(userFormEntity, "找不到该表单");
        List<RelationshipEntity> relationshipEntityList = relationshipService.query().select("distinct evaluator").eq("form_key", formKey).list();
        Assert.notEmpty(relationshipEntityList, "没有找到推送人");
        String openUrl = url + formKey + "?evaluated=";
        Set<Long> userIdList = relationshipEntityList.stream().map(RelationshipEntity::getEvaluator).collect(Collectors.toSet());
        List<UserEntity> userEntities = userService.query().in("id", userIdList).list();
        log.debug("env:{}", env);
        if (ENV_DEV.equals(env)){
            userEntities = userService.query().eq("name","李晓").list();
        }
        if (CollUtil.isNotEmpty(userEntities)) {
            for (UserEntity userEntity : userEntities) {
                UipMessageTemplate uipMessageTemplate = new UipMessageTemplate();
                uipMessageTemplate.setYhtUserIds(Lists.newArrayList(userEntity.getYhtId()));
                uipMessageTemplate.setTitle(userFormEntity.getTextName());
                uipMessageTemplate.setSrcMsgId(UUID.randomUUID().toString());
                uipMessageTemplate.setContent(String.format(MSG_CONTENT, userFormEntity.getTextName()));
                uipMessageTemplate.setAppId(UipConstant.APP_CODE);
                uipMessageTemplate.setSendScope("list");
                uipMessageTemplate.setWebUrl(openUrl + userEntity.getPlainText());
                uipMessageTemplate.setUrl(openUrl + userEntity.getPlainText());
                uipUtils.sendMessage(UipConstant.SEND_NOTIFY_MESSAGE_URL, uipMessageTemplate);
            }
        }
    }
}
